package com.altekis.kanbankaze.shared;

import java.io.Serializable;
import java.util.Date;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Queries;
import javax.jdo.annotations.Query;

import com.google.gwt.user.client.rpc.IsSerializable;

/**
 * Represents a task in the Kanban board
 * 
 * @author gabriprat
 * 
 */
@SuppressWarnings("serial")
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
@Queries({ @Query(name = "findNewer", value = "select from Task o where modificationDate > :minDate") })
public class Task implements
	Serializable, Comparable<Task>, IsSerializable {

	@Persistent
	private String color;
	@Persistent
	private Date createDate;
	@Persistent
	private String description;
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
	private String id;
	@Persistent
	private Date modificationDate;
	@Persistent
	private String name;
	@Persistent
	private String owner;
	@Persistent
	private int priority;
	@Persistent
	private int stage;
	@Persistent
	private Long userStory;

	/**
	 * Default constructor
	 */
	/**
	 * 
	 */
	public Task() {
		super();
	}

	/**
	 * One-call constructor
	 * 
	 * @param color
	 * @param description
	 * @param id
	 * @param name
	 * @param owner
	 * @param priority
	 * @param stage
	 */
	public Task(String color, String name, String description, String owner,
			int stage, int priority) {
		super();
		this.color = color;
		this.name = name;
		this.description = description;
		this.owner = owner;
		this.stage = stage;
		this.priority = priority;
		this.createDate = new Date();
		this.modificationDate = new Date();
	}

	@Override
	public int compareTo(Task o) {
		return this.getId().compareTo(o.getId());
	}

	@Override
	public boolean equals(Object o) {
		boolean ret = false;
		if (o instanceof Task) {
			Task t = (Task) o;
			ret = this.getId().equals(t.getId());
		}
		return ret;
	}

	/**
	 * @return the color
	 */
	public String getColor() {
		return color==null?"yellow":color;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public String getDescription() {
		return description;
	}

	public String getId() {
		return id;
	}

	public Date getModificationDate() {
		return modificationDate;
	}

	public String getName() {
		return name;
	}

	public String getOwner() {
		return owner;
	}

	public int getPriority() {
		return priority;
	}

	public int getStage() {
		return stage;
	}

	/**
	 * @return the userStory
	 */
	public Long getUserStory() {
		return userStory;
	}

	public void onDrop(Stage stage) {
		this.setStage(stage.getOrder());
	}

	/**
	 * @param color the color to set
	 */
	public void setColor(String color) {
		this.color = color;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setModificationDate(Date modificationDate) {
		this.modificationDate = modificationDate;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public void setStage(int stage) {
		this.stage = stage;
	}

	/**
	 * @param userStory
	 *            the userStory to set
	 */
	public void setUserStory(Long userStory) {
		this.userStory = userStory;
	}
}
